package com.dlc.felear.xiaoensale.dao;

import android.content.Context;
import android.util.Log;

import com.dlc.felear.xiaoensale.entity.MenuFoodMaterial;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.dlc.felear.xiaoensale.utils.UuidUtil;
import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MenuFoodMaterialDao {
    private static final String TAG = "MenuFoodMaterialDao";
    private Dao<MenuFoodMaterial, Integer> PipeInfoDao;

    /**
     * 构造方法
     * 获得数据库帮助类实例，通过传入Class对象得到相应的Dao
     *
     * @param context
     */
    public MenuFoodMaterialDao(Context context) {
        try {
            PipeInfoDao = DetailDataOpenHelper.getInstance(context).getDao(MenuFoodMaterial.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加一条记录
     *
     * @param PipeInfo
     */
    public void addOrUpdate(MenuFoodMaterial PipeInfo) {
        try {
            List<MenuFoodMaterial> temp = getMenuFoodMaterialByCidNum(PipeInfo.getCid(), PipeInfo.getNumber());
            if (temp != null&&!temp.isEmpty()) {
                PipeInfoDao.delete(temp);
            }
            PipeInfo.setId(UuidUtil.getUuid());
            PipeInfoDao.create(PipeInfo);
            Log.e(TAG, "结果:" + JsonHelper.toJson(queryForAllByCId(PipeInfo.getCid())));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void addOrUpdateStd(MenuFoodMaterial PipeInfo) {
        try {
            List<MenuFoodMaterial>  data=PipeInfoDao.queryBuilder().where().eq("type",PipeInfo.getType())
                    .and().eq("foodid",PipeInfo.getFoodid()).query();
            if(data==null||data.isEmpty()){
                PipeInfoDao.create(PipeInfo);
            }
//            PipeInfoDao.delete(data);
//            PipeInfoDao.create(PipeInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public void addAll(List<MenuFoodMaterial> list) {
        if (list == null) {
            return;
        }
        for (MenuFoodMaterial MenuFoodMaterial : list) {
            if (MenuFoodMaterial != null) {
                addOrUpdate(MenuFoodMaterial);
            }
        }
    }

    public void addAll(List<MenuFoodMaterial> list,String newId) {
        if (list == null) {
            return;
        }
        deleteForCid(newId);
        for (MenuFoodMaterial MenuFoodMaterial : list) {
            if (MenuFoodMaterial != null) {
                MenuFoodMaterial.setCid(newId);
                addOrUpdate(MenuFoodMaterial);
            }
        }
    }


    /**
     * 删除一条记录
     *
     * @param PipeInfo
     */
    public void delete(MenuFoodMaterial PipeInfo) {
        try {
            int result = PipeInfoDao.delete(PipeInfo);
            Log.e(TAG, "删除：" + JsonHelper.toJson(queryForAllByCId(PipeInfo.getCid())) + " result=" + result);
        } catch (SQLException e) {
            Log.e(TAG, "e=" + e.getMessage());
            e.printStackTrace();
        }
    }

    public void delete(List<MenuFoodMaterial> data) {
        for (MenuFoodMaterial foodMaterial : data) {
            delete(foodMaterial);
        }
    }

    public void deleteForCid(String cid) {
        try {
            List<MenuFoodMaterial> ftemp = queryForAllByCId(cid);
            if (ftemp != null) {
                for (MenuFoodMaterial MenuFoodMaterial : ftemp) {
                    delete(MenuFoodMaterial);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 查询所有记录
     *
     * @return
     */
    public List<MenuFoodMaterial> queryForAll(int type) {
        List<MenuFoodMaterial> PipeInfos = new ArrayList();
        try {
            PipeInfos = PipeInfoDao.queryBuilder().where().eq("type", type).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return PipeInfos;
    }

    public int getFoodIdByName(String name) {
        List<MenuFoodMaterial> PipeInfos;
        try {
            PipeInfos = PipeInfoDao.queryBuilder().where().eq("foodName", name).query();
            if (PipeInfos != null && !PipeInfos.isEmpty()) {
                return PipeInfos.get(0).getFoodid();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 查询所有记录
     *
     * @return
     */
    public List<MenuFoodMaterial> queryForAllByName(String action, int type) {
        List<MenuFoodMaterial> PipeInfos = new ArrayList();
        try {
            PipeInfos = PipeInfoDao.queryBuilder().where().eq("type", type).and().like("foodName", "%" + action + "%").query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return PipeInfos;
    }


    public List<MenuFoodMaterial> queryForAllByCId(String action) {
        List<MenuFoodMaterial> PipeInfos = new ArrayList();
        try {
            PipeInfos = PipeInfoDao.queryBuilder().where().eq("cid", action).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return PipeInfos;
    }

    public MenuFoodMaterial queryForFoodId(int action) {
        MenuFoodMaterial pipeInfos = null;
        try {
            List<MenuFoodMaterial> list = PipeInfoDao.queryBuilder().where().eq("foodid", action).query();
            if (list != null && !list.isEmpty()) {
                pipeInfos = list.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pipeInfos;
    }
    public MenuFoodMaterial queryForFoodId(String id) {
        MenuFoodMaterial pipeInfos = null;
        try {
            List<MenuFoodMaterial> list = PipeInfoDao.queryBuilder().where().eq("id", id).query();
            if (list != null && !list.isEmpty()) {
                pipeInfos = list.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pipeInfos;
    }

    private List<MenuFoodMaterial> getMenuFoodMaterialByCidNum(String cid, int num) {
        List<MenuFoodMaterial> PipeInfos = new ArrayList();
        try {

            PipeInfos.addAll(PipeInfoDao.queryBuilder()
                    .where().eq("cid", cid)
                    .and()
                    .eq("number", num)
                    .query());
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
        if (PipeInfos == null || PipeInfos.isEmpty()) {
            return null;
        }
        return PipeInfos;
    }

}
